package com.qf.mybatis01.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSession_utils {
    //1.把mybatis-config.xml转换成输入流对象
    private static InputStream ras=null;
    static {
    {
        try {
            ras = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }
    public static SqlSession getSqlSession(){
        //2.创建一个SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //3.利用sqlSessionFactory 对象可以创建Sqlsession
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(ras);
        //4.核心对象 sqlSession
        SqlSession  sqlSession=sqlSessionFactory.openSession();
        return sqlSession;
    }
}
